/*!
 * @file GameActuator.cpp
 * @author Rocco Martino
 */
/***************************************************************************
 *   Copyright (C) 2014 by Rocco Martino                                   *
 *   martinorocco@gmail.com                                                *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU Lesser General Public License as        *
 *   published by the Free Software Foundation; either version 2.1 of the  *
 *   License, or (at your option) any later version.                       *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU Lesser General Public      *
 *   License along with this program; if not, write to the                 *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

/* ======================================================================= */
/* ....................................................................... */
#include <ooGame/GameActuator>
#include <ooGame/EditObjectActuator>
#include <ooGame/Game>

#include <osgODE/World>
#include <osgODE/RigidBody>
/* ....................................................................... */
/* ======================================================================= */




using namespace ooGame ;




/* ======================================================================= */
/* ....................................................................... */
GameActuator::GameActuator(void):
    m_mode          ( QUIT )
{
}
/* ....................................................................... */
/* ======================================================================= */




/* ======================================================================= */
/* ....................................................................... */
GameActuator::GameActuator(const GameActuator& other, const osg::CopyOp& copyop):
    Actuator        ( other, copyop ),
    m_mode          ( other.m_mode ),
    m_file_name     ( other.m_file_name ),
    m_handler       ( other.m_handler )
{
}
/* ....................................................................... */
/* ======================================================================= */




/* ======================================================================= */
/* ....................................................................... */
GameActuator::~GameActuator(void)
{
}
/* ....................................................................... */
/* ======================================================================= */




/* ======================================================================= */
/* ....................................................................... */
void
GameActuator::actuate( osgODE::ODEObject* object, Game* game )
{
    (void) object ;
    (void) game ;


    if( m_handler.valid() ) {

        (*m_handler) ( this ) ;

    }
}
/* ....................................................................... */
/* ======================================================================= */




/* ======================================================================= */
/* ....................................................................... */
namespace {
    void    set_handler( osgODE::ODEObject* obj, GameActuator::Handler* handler )
    {

        osgODE::ODECallback*    cbk = obj->getUpdateCallback() ;

        while( cbk ) {
            ooGame::Game*   game = dynamic_cast<Game*>( cbk ) ;

            if( game ) {

                for( unsigned int i=0; i<game->getActuatorList().size(); i++ ) {

                    Actuator*   act = game->getActuatorList()[i].get() ;

                    GameActuator*  sa = dynamic_cast<GameActuator*>( act ) ;

                    if( sa ) {
                        sa->setHandler( handler ) ;
                    }



                    if( dynamic_cast<EditObjectActuator*>( act ) ) {
                        osgODE::ODEObject*  obj = static_cast<EditObjectActuator*>( act )->getObject() ;

                        if( obj ) {
                            set_handler( obj, handler ) ;
                        }
                    }
                }

            }

            cbk = cbk->getNestedCallback() ;
        }
    }
}
/* ....................................................................... */
/* ======================================================================= */




/* ======================================================================= */
/* ....................................................................... */
void
ooGame::setGameActuatorHandler( osgODE::World* world, GameActuator::Handler* handler )
{

    osgODE::World::ObjectList& objects = world->getObjectList() ;

    for( unsigned int i=0; i<objects.size(); i++ ) {

        set_handler( objects[i].get(), handler ) ;
    }
}
/* ....................................................................... */
/* ======================================================================= */
